package cn.maihe.elg.operation.centers.hymake.channel.service;

import cn.maihe.elg.operation.centers.hymake.auth.HyMakeCenterAuthConfig;
import cn.maihe.elg.operation.centers.hymake.channel.HyMakeBaseChannelService;
import cn.maihe.elg.operation.centers.hymake.dto.channel.ChlBizRespData;
import cn.maihe.elg.operation.centers.hymake.dto.channel.PayResultReceiveReqDTO;
import cn.maihe.elg.operation.centers.hymake.enums.HyMakeChannelMethod;
import cn.maihe.elg.operation.centers.hymake.enums.HyMakeRespCodeEnum;
import cn.maihe.elg.operation.centers.supports.model.bo.NoticeGuaranteeApplyResultBo;
import cn.maihe.elg.operation.centers.supports.model.bo.NoticePayResultBo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.BidCustomerDO;
import cn.maihe.elg.operation.service.business.BidCustomerService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description 出函机构支付确认到账后通知 电子保函至平台侧
 * @Author WJH
 * @Date 2021/04/21
 */
@Service
@Slf4j
public class HyMakePayResultReceiveChannelService extends HyMakeBaseChannelService<PayResultReceiveReqDTO, ChlBizRespData> {

    @Resource
    private ElgResourceService elgResourceService;
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private BidCustomerService bidCustomerService;

    @Override
    protected HyMakeChannelMethod methodEnum() {
        return HyMakeChannelMethod.payResultReceive;
    }


    public InnerBaseResp<NoticeGuaranteeApplyResultBo> noticePayResult(NoticePayResultBo bo) {
        AcceptOrderInfoDO acceptOrderInfo = acceptOrderInfoService.getByAcceptOrderNo(bo.getAcceptOrderNo());
        Assert.notNull(acceptOrderInfo, "查无此投保单号记录");

        BidCustomerDO bidCustomer = bidCustomerService.selectById(acceptOrderInfo.getBidCustomerId());

        PayResultReceiveReqDTO reqDTO = new PayResultReceiveReqDTO();
        reqDTO.setInsuranceNo(acceptOrderInfo.getAcceptOrderNo());
        reqDTO.setPayResult(bo.getPayResult());
        reqDTO.setPayResultMsg(bo.getPayResultMsg());
        //fixme 支付订单号
        reqDTO.setTradeNo(null);
        reqDTO.setPayType("1");
        reqDTO.setPayAccountName(bidCustomer.getBankUserName());
        reqDTO.setPayAccountNo(bidCustomer.getBankCardNum());
        reqDTO.setPayPhone(bidCustomer.getBidderContactTel());
        reqDTO.setPayBankOpenBank(bidCustomer.getBankName());
        reqDTO.setPolicyNo(acceptOrderInfo.getGuaranteeNo());

        InnerBaseResp<NoticeGuaranteeApplyResultBo> innerBaseResp;


        CenterNoEnum centerNoEnum = this.getCenterNoEnumById(acceptOrderInfo.getCenterId());
        try {
            ChlBizRespData chlBizRespData;
            HyMakeCenterAuthConfig authConfig = hyMakeInfoConfig.getAuthConfig(centerNoEnum);
            if ("0".equals(authConfig.getCenterInfo().getSyncPayResult())) {
                chlBizRespData = new ChlBizRespData();
                chlBizRespData.setCode("0");
                chlBizRespData.setMessage("已开启不推送支付结果到中心");
            } else {
                chlBizRespData = this.doRequest(centerNoEnum, reqDTO);
            }
            bo.setRespData(chlBizRespData);
            //code=10已回调成功作为成功处理 {"code":"10","message":"该保函已经成功回调过了"}
            if (HyMakeRespCodeEnum.isSuccess(chlBizRespData.getCode()) ||
                    "10".equals(chlBizRespData.getCode())
            ) {
                innerBaseResp = InnerBaseResp.success(bo);
            } else {
                innerBaseResp = InnerBaseResp.error(bo, chlBizRespData.getMessage());
            }
        } catch (Exception e) {
            log.error("支付结果通知 >> [{}] 异常:{}", centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("支付结果通知异常:" + e.getMessage());
        }

        return innerBaseResp;

    }
}
